﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Helpers;
using System.Collections.ObjectModel;

namespace ISBNScanner
{
    public class TitleAndCopies : NotifyPropertyChangedBase
    {
        string _isbn;
        public string Isbn
        {
            get { return _isbn; }
            set
            {
                _isbn = value;
                NotifyPropertyChanged("Isbn");
            }
        }

        ObservableCollection<string> _copies;
        public ObservableCollection<string> Copies
        {
            get { return _copies; }
            set
            {
                _copies = value;
                NotifyPropertyChanged("Copies");
            }
        }

        public TitleAndCopies()
        {
            Copies = new ObservableCollection<string>();
        }

        public override string ToString()
        {
            return Isbn + ":" + string.Join(",", Copies);
        }

        public static TitleAndCopies Parse(string data)
        {
            int p = data.IndexOf(':');
            string isbn = "";
            if (p > 0)
            {
                isbn = data.Substring(0, p - 1);

                return new TitleAndCopies
                {
                    Isbn = isbn,
                    Copies = new ObservableCollection<string>(data.Substring(p + 1).Split(','))
                };
            }
            else
                if (data != "")
                {
                    return new TitleAndCopies { Isbn = data };
                }
            return null;
        }
    }
}
